package com.lh.job.task;

import com.lh.common.entity.admin.Period;
import com.lh.job.service.PeriodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author FH
 * @ClassName: RobotBuyTask
 * @Description: (机器人下单)
 * @date: 2020-05-22 16:01
 */
@Component("robotBuyTask")
@Slf4j
public class RobotBuyTask implements ITask{

    @Autowired
    PeriodService periodService;

    /**
     * 机器人下单
     */
    @Transactional
    @Override
    public void run(String params) {
        List<Period> periods = periodService.queryPeriodList();
        for(Period period : periods){
            int totalPortion = period.getTotalPortion();
            if(totalPortion > 0 && totalPortion >= 16){
                period.setTotalPortion(period.getTotalPortion()-16);
                period.setCurrenPortion(period.getCurrenPortion()+16);
                period.setRobotPortion(period.getRobotPortion()+16);
                periodService.updatePeriodRobotBuy(period);
            }else if(totalPortion > 0 && totalPortion < 16){
                period.setTotalPortion(period.getTotalPortion()-totalPortion);
                period.setCurrenPortion(period.getCurrenPortion()+totalPortion);
                period.setRobotPortion(period.getRobotPortion()+totalPortion);
                periodService.updatePeriodRobotBuy(period);
            }
        }
    }
}
